#include "CDisplayWidget.h"

#include <QDebug>
#include <QPalette>
#include <QPainter>
#include <QMouseEvent>

CDisplayWidget::CDisplayWidget(QWidget *parent) : QWidget(parent)
{
    this->resize(1000, 1000);

    this->initWidget();

    this->setMouseTracking(false);

    m_size_x = this->size().width();
    m_size_y = this->size().height();

    m_paint_origin_line = true;
    m_paint_bezier_curve = true;
}

void CDisplayWidget::initWidget()
{
    /* 设置白色背景 */
    QPalette palette(this->palette());
    palette.setColor(QPalette::Background, QColor(255, 255, 255));
    this->setAutoFillBackground(true);
    this->setPalette(palette);
}

void CDisplayWidget::paintGrid(QPainter &painter)
{
    painter.setPen(QPen(Qt::lightGray, 1));

    for (quint16 i = 0; i <= m_size_x; i += 50)
    {
        QPoint start(i, 0);
        QPoint end(i, m_size_y);
        painter.drawLine(start, end);
    }

    for (quint16 i = 0; i <= m_size_y; i += 50)
    {
        QPoint start(0, i);
        QPoint end(m_size_y, i);
        painter.drawLine(start, end);
    }
}

void CDisplayWidget::paintClickPointList(QPainter &painter)
{
    for (int i = 0; i < m_clicked_points.size(); i ++)
    {

        if (m_paint_origin_line && i < m_clicked_points.size() - 1)
        {
            QPoint start = m_clicked_points[i];
            QPoint end = m_clicked_points[i + 1];
            painter.setPen(QPen(Qt::gray, 5));
            painter.drawLine(start, end);
        }

        painter.setPen(QPen(Qt::green, 10));
        painter.drawPoint(m_clicked_points.at(i));
    }
}

void CDisplayWidget::paintBezierCurve(QPainter &painter)
{
    for (auto &each : m_two_order_bezier_curve_points)
    {
        painter.setPen(QPen(Qt::red, 5));
        painter.drawPoint(each);
    }

    for (auto &each : m_three_order_bezier_curve_points)
    {
        painter.setPen(QPen(Qt::darkBlue, 5));
        painter.drawPoint(each);
    }
}

void CDisplayWidget::paintTwoOrderRollLine(QPainter &painter)
{
    if (!m_two_bezier_roll_line_start.isNull())
    {
        painter.setPen(QPen(Qt::blue, 2));
        painter.drawLine(m_two_bezier_roll_line_start, m_two_bezier_roll_line_end);

        painter.setPen(QPen(Qt::red, 2));
        QRectF rectangle1(m_two_bezier_roll_line_start.x() - 7.0, m_two_bezier_roll_line_start.y() - 7.0, 14.0, 14.0);
        painter.drawEllipse(rectangle1);

        QRectF rectangle2(m_two_bezier_roll_line_end.x() - 7.0, m_two_bezier_roll_line_end.y() - 7.0, 14.0, 14.0);
        painter.drawEllipse(rectangle2);
    }

}

void CDisplayWidget::paintThreeOrderRollLine(QPainter &painter)
{
    if (!m_three_bezier_roll_line_1_start.isNull())
    {
        painter.setPen(QPen(Qt::blue, 2));
        painter.drawLine(m_three_bezier_roll_line_1_start, m_three_bezier_roll_line_1_end);
        painter.setPen(QPen(Qt::darkRed, 2));
        painter.drawLine(m_three_bezier_roll_line_2_start, m_three_bezier_roll_line_2_end);
        painter.setPen(QPen(Qt::lightGray, 2));
        painter.drawLine(m_three_bezier_roll_line_3_start, m_three_bezier_roll_line_3_end);

        painter.setPen(QPen(Qt::red, 2));
        QRectF rectangle1(m_three_bezier_roll_line_1_start.x() - 7.0, m_three_bezier_roll_line_1_start.y() - 7.0, 14.0, 14.0);
        painter.drawEllipse(rectangle1);

        QRectF rectangle2(m_three_bezier_roll_line_1_end.x() - 7.0, m_three_bezier_roll_line_1_end.y() - 7.0, 14.0, 14.0);
        painter.drawEllipse(rectangle2);

        painter.setPen(QPen(Qt::red, 2));
        QRectF rectangle3(m_three_bezier_roll_line_2_start.x() - 7.0, m_three_bezier_roll_line_2_start.y() - 7.0, 14.0, 14.0);
        painter.drawEllipse(rectangle1);

        QRectF rectangle4(m_three_bezier_roll_line_2_end.x() - 7.0, m_three_bezier_roll_line_2_end.y() - 7.0, 14.0, 14.0);
        painter.drawEllipse(rectangle2);

        painter.setPen(QPen(Qt::red, 2));
        QRectF rectangle5(m_three_bezier_roll_line_3_start.x() - 7.0, m_three_bezier_roll_line_3_start.y() - 7.0, 14.0, 14.0);
        painter.drawEllipse(rectangle1);

        QRectF rectangle6(m_three_bezier_roll_line_3_end.x() - 7.0, m_three_bezier_roll_line_3_end.y() - 7.0, 14.0, 14.0);
        painter.drawEllipse(rectangle2);
    }
}

void CDisplayWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);

    paintGrid(painter);

    paintClickPointList(painter);

    if (m_paint_bezier_curve)
    {
        paintBezierCurve(painter);
    }

    paintTwoOrderRollLine(painter);

    paintThreeOrderRollLine(painter);

    update();
}

void CDisplayWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        qDebug() << "mousePressEvent -> LeftButton" << endl;

        QPointF click_pos = event->localPos();
        QPoint  pos = click_pos.toPoint();

        emit signalSendMouseLeftClickedPoint(pos);
        m_clicked_points.push_back(pos);
    }
    else if (event->button() == Qt::RightButton)
    {
        qDebug() << "mousePressEvent -> RightButton" << endl;
        m_clicked_points.clear();
        m_two_order_bezier_curve_points.clear();
        m_three_order_bezier_curve_points.clear();

        m_two_bezier_roll_line_start.setX(0);
        m_two_bezier_roll_line_start.setY(0);

        m_three_bezier_roll_line_1_start.setX(0);
        m_three_bezier_roll_line_1_start.setY(0);
        emit signalClearAll();
    }
}

void CDisplayWidget::slotClearAll()
{
    m_clicked_points.clear();
    m_two_order_bezier_curve_points.clear();
    m_three_order_bezier_curve_points.clear();

    m_two_bezier_roll_line_start.setX(0);
    m_two_bezier_roll_line_start.setY(0);

    m_three_bezier_roll_line_1_start.setX(0);
    m_three_bezier_roll_line_1_start.setY(0);
}

void CDisplayWidget::slotRecvTwoOrderBezierPoints(QPoint point)
{
    m_two_order_bezier_curve_points.push_back(point);
}

void CDisplayWidget::slotRecvThreeOrderBezierPoints(QPoint point)
{
    m_three_order_bezier_curve_points.push_back(point);
}

void CDisplayWidget::slotRecvTwoBezierRollLine(QPoint start_point, QPoint end_point)
{
    m_two_bezier_roll_line_start = start_point;
    m_two_bezier_roll_line_end   = end_point;
}

void CDisplayWidget::slotRecvThreeBezierRollLine(QPoint point1, QPoint point2, QPoint point3, QPoint point4, QPoint point5, QPoint point6)
{
    m_three_bezier_roll_line_1_start = point1;
    m_three_bezier_roll_line_1_end   = point2;

    m_three_bezier_roll_line_2_start = point3;
    m_three_bezier_roll_line_2_end   = point4;

    m_three_bezier_roll_line_3_start = point5;
    m_three_bezier_roll_line_3_end   = point6;
}

void CDisplayWidget::slotRecvPaintOriginLineFlag(bool flag)
{
    m_paint_origin_line = flag;
}

void CDisplayWidget::slotRecvPaintCurveFlag(bool flag)
{
    m_paint_bezier_curve = flag;
}
